<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\Shop;
use app\admin\model\Shopcharge;
use app\admin\model\Shopdevice;
use app\admin\model\Shoporder;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproom;
use app\admin\model\Shoptableorder;
use app\admin\model\Tuanorder;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {

        $userModel = new User();
        $totalUser = $userModel->count('id');
        $todayUser = $userModel->whereTime('createtime', 'today')->count('id');

        $shopOrderModel = new Shoporder();
        $chargeModel = new Shopcharge();
        $tuanOrderModel = new Tuanorder();
        $shopTableOrderModel = new Shoptableorder();

        $todayQpOrder = $shopOrderModel->where('status', 'not in', ['paying', 'refund'])->whereTime('create_time', 'today')->count('id');
        $todayChargeOrder = $chargeModel->where('status', 'not in', ['paying', 'refund'])->whereTime('create_time', 'today')->count('id');
        $todayTuanOrder = $tuanOrderModel->where('status', 'not in', ['paying', 'refund'])->whereTime('create_time', 'today')->count('id');
        $todayTableOrder = $shopTableOrderModel->where('status', 'not in', ['paying', 'refund'])->whereTime('create_time', 'today')->count('id');

        $todayOrder = $todayQpOrder + $todayChargeOrder + $todayTuanOrder + $todayTableOrder;

        $this->view->assign("todayOrder", $todayOrder);
        $this->view->assign("todayQpOrder", $todayQpOrder);
        $this->view->assign("todayChargeOrder", $todayChargeOrder);
        $this->view->assign("todayTableOrder", $todayTableOrder);
        $this->view->assign("todayTuanOrder", $todayTuanOrder);


        $todayQpAmount = $shopOrderModel->where('status', 'not in', ['paying', 'refund'])->whereTime('create_time', 'today')->sum('amount');
        $todayChargeAmount = $chargeModel->where('status', 'not in', ['paying', 'refund'])->whereTime('create_time', 'today')->sum('amount');
        $todayTableAmount = $shopTableOrderModel->where('status', 'not in', ['paying', 'refund'])->whereTime('create_time', 'today')->sum('pay_amount');

        $this->view->assign("todayQpAmount", $todayQpAmount);
        $this->view->assign("todayChargeAmount", $todayChargeAmount);
        $this->view->assign("todayTableAmount", $todayTableAmount);


        $totalOrder = 0;
        $totalAmount = $shopOrderModel->where('status', 'not in', ['paying', 'refund'])->whereTime('create_time', 'today')->sum('amount');

        $chargeOrderModel = new Shopcharge();
        $chargeAmount = $chargeOrderModel->where('status', 'not in', ['paying', 'refund'])->whereTime('create_time', 'today')->sum('amount');

        $shopModel = new Shop();
        $totalShop = $shopModel->where('is_fake', -1)->count('id');//排除虚拟店铺
        $openShop = $shopModel->where('is_fake', -1)->where('status', 'in', [1, 2])->count('id');
        $waitShop = $shopModel->where('is_fake', -1)->where('status', 3)->count('id');
        $closeShop = $shopModel->where('is_fake', -1)->where('status', 4)->count('id');

        $this->view->assign("openShop", $openShop);
        $this->view->assign("waitShop", $waitShop);
        $this->view->assign("closeShop", $closeShop);

        $shopRoomModel = new Shoproom();
        $rooms = $shopRoomModel->count('id');

        $shopDeviceModel = new Shopdevice();
        $offline = $shopDeviceModel->where('status', 'offline')->count('id');
        $this->view->assign("totalOrder", $totalOrder);
        $this->view->assign("totalAmount", $totalAmount);
        $this->view->assign("todayCharge", $chargeAmount);
        $this->view->assign("totalShop", $totalShop);
        $this->view->assign("offline", $offline);
        $this->view->assign("room", $rooms);
        $this->view->assign("totalUser", $totalUser);
        $this->view->assign("todayUser", $todayUser);


        $shopOrderProfitModel = new Shoporderprofit();
        $sendAmount = $shopOrderProfitModel->whereTime('create_time', 'today')->sum('amount');
        $this->view->assign("sendAmount", $sendAmount);


        return $this->view->fetch();
    }

}
